#include "stdafx.h"
#include "resource.h"

#include "MainDlg.h"

#include "Registry.h"

BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
{
	return CWindow::IsDialogMessage(pMsg);
}

BOOL CMainDlg::OnIdle()
{
	return FALSE;
}

LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// center the dialog on the screen
	CenterWindow();

	// set icons
	HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),
																	 IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
	SetIcon(hIcon, TRUE);
	HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),
																				IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
	SetIcon(hIconSmall, FALSE);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	UIAddChildWindowContainer(m_hWnd);

	// get default pictures folder
	TCHAR szPath[MAX_PATH] = {0};
	SHGetFolderPath(m_hWnd, CSIDL_MYPICTURES, NULL, 0, szPath);

	m_strFolder = szPath;

	//
	CRegistry reg(HKEY_CURRENT_USER, _T("Software\\NRG\\WallpaperSwitcher"));
	CString strFolder = reg.ReadString(_T("Folder"), m_strFolder);

	//
	m_strFolder = strFolder;

	//
	DoDataExchange(FALSE);

	return TRUE;
}

/**
 *
 *
 */
LRESULT CMainDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	DoDataExchange(TRUE);

	// write
	CRegistry reg(HKEY_CURRENT_USER, _T("Software\\NRG\\WallpaperSwitcher"));
	reg.WriteString(_T("Folder"), m_strFolder);

	CloseDialog(wID);

	return 0;
}

/**
 *
 *
 */
LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CloseDialog(wID);
	return 0;
}

/**
 *
 *
 */
void CMainDlg::CloseDialog(int nVal)
{
	DestroyWindow();
	::PostQuitMessage(nVal);
}

/**
 * @brief OnBrowse
 *
 * CMainDlg::OnBrowse
 *
 * @param WORD wNotifyCode
 * @param WORD wID
 * @param HWND hWndCtl
 * @param BOOL & bHandled
 * @return LRESULT
 */
LRESULT CMainDlg::OnBrowse( WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled )
{
	DoDataExchange(TRUE);

	BROWSEINFO m_bi = {0};

	//HRESULT hResult = S_FALSE;
	//CComPtr < IShellFolder > spDeskFolder;

	//hResult = ::SHGetDesktopFolder ( &spDeskFolder );

	//if ( hResult == S_OK )
	//{
	//	LPITEMIDLIST pidlRoot = NULL;
	//	LPTSTR pszRoot = (LPTSTR)(LPCTSTR)m_strFolder;

	//	// Convert the path to an ITEMIDLIST:

	//	USES_CONVERSION;

	//	hResult = spDeskFolder->ParseDisplayName ( NULL, NULL, T2W ( pszRoot ), NULL, &pidlRoot, NULL );

	//	if ( hResult == S_OK )
	//	{
	//		if ( m_bi.pidlRoot != NULL )
	//			::CoTaskMemFree ( ( LPVOID ) m_bi.pidlRoot );

	//		m_bi.pidlRoot = pidlRoot;
	//	}
	//}

	LPITEMIDLIST pItemIDList = ::SHBrowseForFolder ( &m_bi );

	if ( pItemIDList )
	{

		TCHAR m_szFolPath[ MAX_PATH ] = {0};
		if ( ::SHGetPathFromIDList ( pItemIDList, m_szFolPath ) )
		{
			m_strFolder = m_szFolPath;

			DoDataExchange(FALSE);
		}

		if ( pItemIDList != NULL )
			::CoTaskMemFree ( pItemIDList );
	}

	return 0;
}